#!/usr/local/bin/python
# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import User, Group

class Item(models.Model):
    date = models.DateTimeField(verbose_name='Date achat', null=True, blank=True)
    label = models.CharField(max_length=100, verbose_name='Label')
    activite = models.ForeignKey('Activite', verbose_name='Activité')
    achat = models.ForeignKey('Operation', verbose_name='règlement')
    image = models.ImageField(null=True, blank=True, upload_to='images')
    
    def dispo(self):
        q = Emprunt.objects.filter(item=self, end__isnull=True)
        return (q.count()==0)
    
    def __unicode__(self):
        return self.label

class SubItem(models.Model):
    label = models.CharField(max_length=100, verbose_name='Label')
    item = models.ForeignKey(Item, verbose_name='Parent', related_name='subitems')
    image = models.ImageField(null=True, blank=True, upload_to='images')
    def __unicode__(self):
        return self.label

class Emprunt(models.Model):
    date = models.DateTimeField(auto_now_add=True)
    start = models.DateTimeField(verbose_name='Date emprunt')
    endPlan = models.DateTimeField(null=True, blank=True, verbose_name='Date retour prévue')
    end = models.DateTimeField(null=True, blank=True, verbose_name='Date retour effective')
    item = models.ForeignKey(Item, verbose_name='Element emprunté')
    membre = models.ForeignKey('Membre', null=True, blank=True, verbose_name='Emprunteur')
    
    def __init__(self, *args, **kwargs):
        from datetime import datetime
        super(Emprunt, self).__init__(*args, **kwargs)
        self.subitem_controles = []
        '''
        if create_ctrls:
            for sub in self.item.subitems.all():
                ctrl = Controle(subitem=sub, emprunt=self)
                self.subitem_controles.append(ctrl)
        '''
    def save(self, membre=None):
        super(Emprunt, self).save()
        

class Controle(models.Model):
    subitem = models.ForeignKey(SubItem, null=True, blank=True, verbose_name='Element')
    emprunt = models.ForeignKey(Emprunt, verbose_name='Emprunt', related_name='controles')
    start = models.BooleanField(verbose_name='contrôle départ', default=False)
    end = models.BooleanField(verbose_name='contrôle retour', default=False)
    membre = models.ForeignKey('Membre', verbose_name='contôleur')
    class Meta:
        verbose_name = u'Contrôle'
    
class Activite(models.Model):
    label = models.CharField(max_length=100, verbose_name='Label')
    responsible = models.ForeignKey('Membre', null=True, blank=True, verbose_name='Responsable')
    groupMembers = models.ForeignKey(Group, related_name='member_activites', null=True, blank=True, verbose_name='groupe des membres')
    groupManagers = models.ForeignKey(Group, related_name='manager_activites', null=True, blank=True, verbose_name='groupe des managers')
    
    def is_manager(self, user):
        group = self.groupManagers
        if group in user.groups.all():
            return True
        return False
    
    def __unicode__(self):
        return self.label
    
    class Meta:
        verbose_name = u'Activité'
        permissions = (
            ("can_manage", "responsable"),
        )        

class Membre(models.Model):
    user = models.ForeignKey(User, null=True, blank=True, verbose_name='membre')
    image = models.ImageField(null=True, blank=True, upload_to='images')
    def __unicode__(self):
        return str(self.user)

class Compte(models.Model):
    activite = models.ForeignKey('Activite', verbose_name='Activité')
    def __unicode__(self):
        return self.activite.label

class Operation(models.Model):
    date = models.DateTimeField(verbose_name='Date', null=True, blank=True)
    dateCheck = models.DateTimeField(verbose_name='Contrôle', null=True, blank=True)
    compte = models.ForeignKey(Compte, verbose_name='Compte')
    membre = models.ForeignKey(Membre, null=True, blank=True, verbose_name='membre')
    value = models.DecimalField(max_digits=6, decimal_places=2, verbose_name='valeur', help_text='positif: entrant dans le compte')
    
    def __unicode__(self):
        return '%f' % self.value
    
    class Meta:
        verbose_name = u'Opération'

